package com.qfedu.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @author lenovo
 * @version 1.0
 * @date 2023/4/18 10:51
 * @poject bloo
 */
public class BaseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String action = req.getParameter("action");
            if (action==null){
                req.setAttribute("message","系统忙！");
                req.getRequestDispatcher("/error.jsp").forward(req,resp);
            }else{
                //获取字节码文件对象 1、Class.forName("全路径类名") 2、类名.class  3、对象.getClass
                //通过暴力反射获取方法对象
                Method method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
                //执行方法  第一个参数：要执行的对象， 第二个参数：可变的方法参数数组
                method.invoke(this, req, resp);
            }

        } catch (Exception e) {
            req.setAttribute("message","系统忙！");
            req.getRequestDispatcher("/error.jsp").forward(req,resp);
        }

    }
}
